using System.Collections;
using System.Collections.Generic;
using EventUtils;
using UnityEngine;
using UnityEngine.UI;
using EventType = EventUtils.EventType;

namespace View.Instant
{
    public class ShopView : View
    {
        private Button mbtnClose;
        private GameObject mobjItem;
        private Transform mtranParent;
        private ScrollRect mScrollrect;
        private PropItemEnum mPropItem = PropItemEnum.none;
        public override void OnLoad()
        {
            base.OnLoad();
            
            mbtnClose = transform.Find("btnClose").GetComponent<Button>();
            mobjItem =  transform.Find("item").gameObject;
            mtranParent = transform.Find("ScrollView/Viewport/Content").transform;
            mScrollrect = transform.Find("ScrollView").GetComponent<ScrollRect>();

            var jinbi = GameLocalInfo.OnGetPropItemTableById(PropItemEnum.jinbi);
            GameObject go1 = GameObject.Instantiate(mobjItem);
            go1.transform.SetParent(mtranParent);
            go1.transform.localPosition = Vector3.zero;
            go1.transform.localScale = Vector3.one;
            go1.transform.localRotation = Quaternion.identity;
            go1.SetActive(true);
            var image1 = go1.transform.Find("Image").GetComponent<Image>();
            image1.sprite = ResourceManager.Instance.LoadSprite(jinbi.path, jinbi.iconname);
            image1.SetNativeSize();
            
            go1.transform.Find("Image/title").GetComponent<Text>().text = jinbi.itemname;
            go1.transform.Find("Image/txtcount").GetComponent<Text>().text = string.Format("x{0}",jinbi.count);
            
            go1.transform.Find("btnbuy").gameObject.SetActive(false);
            AddClickListener(go1.transform.Find("btnsee").GetComponent<Button>(), () =>
            {
                OnBtn_ClickAdv(PropItemEnum.jinbi);
            });
            
            var allitem = GameLocalInfo.OnGetPropItemList();
            for (int i = 0; i < allitem.Count; i++)
            {
                if (allitem[i].itemType == 1)
                {
                    GameObject go = GameObject.Instantiate(mobjItem);
                    go.transform.SetParent(mtranParent);
                    go.transform.localPosition = Vector3.zero;
                    go.transform.localScale = Vector3.one;
                    go.transform.localRotation = Quaternion.identity;
                    go.SetActive(true);

                    var image = go.transform.Find("Image").GetComponent<Image>();
                    image.sprite = ResourceManager.Instance.LoadSprite(allitem[i].path, allitem[i].iconname);
                    image.SetNativeSize();
                    // go.transform.Find("Image").GetComponent<Image>().sprite = 
                    go.transform.Find("Image/title").GetComponent<Text>().text = allitem[i].itemname;
                    go.transform.Find("Image/txtcount").GetComponent<Text>().text = string.Format("x{0}",allitem[i].count);
                    go.transform.Find("btnbuy/need").GetComponent<Text>().text = allitem[i].buyneed.ToString();

                    int buyneed = allitem[i].buyneed;
                    PropItemEnum enumid = allitem[i].enumid;
                    AddClickListener(go.transform.Find("btnbuy").GetComponent<Button>(), () => { OnBtn_ClickGoldBuy(buyneed,enumid); });
                    AddClickListener(go.transform.Find("btnsee").GetComponent<Button>(), () => { OnBtn_ClickAdv(enumid); });
                }
            }
            
            AddClickListener(mbtnClose,() =>
            {
                UIManager.Instance.Close(ViewName.ShopView);
            });
            
            mScrollrect.verticalNormalizedPosition = 1;
        }

        private void OnBtn_ClickAdv(PropItemEnum pro)
        {
            //去看广告   回来之后获得这些
            mPropItem = pro;
            Debug.LogError("点击："+pro);
            UIManager.Instance.OnShowLoading();
            AndroidManager.Ins.LoadReward(() =>
            {
                UIManager.Instance.OnCloseLoading();
                Debug.LogError("看完回应");
                TaskManager.OnAddTaskNumByTaskType(TaskTypeEnum.adv);
                EventCenter.BroadCast<string>(EventType.EVENT_SEE_ADV_END, ViewName.ShopView);
            });
        }
        private void OnBtn_ClickGoldBuy(int buyneed,PropItemEnum enumid)
        {
            if (GameLocalInfo.mPlayerGold >= buyneed)
            {
                GameLocalInfo.OnUpdatePlayerGold(buyneed*-1);
                EventCenter.BroadCast(EventType.EVNET_UPDATE_PLAYERINFO);
                GameHelp.OnAddSelfPlayerPropItem(GameLocalInfo.OnGetPropItemTableCloneById(enumid));
                EventCenter.BroadCast<List<PropItemEnum>>(EventType.EVENT_GONGXIHUODE,new List<PropItemEnum>(){enumid});
            }
            else
            {
                UIManager.Instance.Show(ViewName.ToolTipsView, ViewLayer.Top, "金币不足！");
                //提示钱不够
            }
        }
        
        private void OnSeeAdvCallback(string str)
        {
            if (str == ViewName.ShopView && mPropItem != PropItemEnum.none)
            {
                var propitem = GameLocalInfo.OnGetPropItemTableCloneById(mPropItem);
                GameHelp.OnAddSelfPlayerPropItem(propitem);
                
                EventCenter.BroadCast<List<PropItemEnum>>(EventType.EVENT_GONGXIHUODE,new List<PropItemEnum>(){propitem.enumid});
            }
        }

        public override void OnShow(params object[] args)
        {
            base.OnShow();
            mScrollrect.verticalNormalizedPosition = 1;
            EventCenter.AddListener<string>(EventType.EVENT_SEE_ADV_END,OnSeeAdvCallback);
        }

        public override void OnClose(params object[] args)
        {
            base.OnClose();
            if (gameObject.activeSelf)
            {
                EventCenter.RemoveListener<string>(EventType.EVENT_SEE_ADV_END,OnSeeAdvCallback);
            }
        }
    }
}

